/**
 * Created by Qiao.Yin on 2016/9/28.
 */
"use strict";
var swiper = new Swiper('.swiper-container');
var tabBtn = $('.swi-tab');
var tabItem = $('.swi-item');
tabBtn.on('click', function () {
    var index = $(this).index();
    common.currItemIndex = index;
    $(this).addClass('curr').siblings().removeClass('curr');
    tabItem.eq(index).removeClass('hide').siblings().addClass('hide');
});
$(window).on('scroll', function () {
    var currItem = tabItem.eq(common.currItemIndex ? common.currItemIndex : 0);
    var page = parseInt(currItem.attr('page'));
    var pageSize = parseInt(currItem.attr('pagesize'));
    var showType = currItem.attr('showtype');
    var noMore = currItem.attr('nomore');
    var scrollTop = 0, clientHeight = 0, scrollHeight = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
        scrollTop = document.documentElement.scrollTop;
    } else if (document.body) {
        scrollTop = document.body.scrollTop;
    }
    if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
    } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
    }
    scrollHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
    // 滚动到底部的时候
    if (scrollTop + clientHeight == scrollHeight && !common.isLoadNextPage ) {
        if(!noMore){
            common.isLoadNextPage = true;
            $.ajax({
                url: 'getItemPagesIF',
                type: 'post',
                data: {
                    'Page': ++page,
                    "ShowType": showType,
                    "PageSize": pageSize
                },
                beforeSend: function () {
                    // layer.load();
                }
            }).done(function (data) {
                if(data.length == 0){
                    // $.alert('没有更多了');
                    currItem.attr('nomore',true);
                } else {
                    currItem.attr('page',++page);
                    // var html = "";
                    // for(var i = 0;i <data.length;i++){
                    //
                    // }
                }
            }).always(function () {
                common.isLoadNextPage = false;
            });
        } else {
            // $.alert('没有更多了');
        }
    }
});